//
//  LXSliderView.m
//  微博
//
//  Created by imera on 15/9/19.
//  Copyright (c) 2015年 imera. All rights reserved.
//

#import "LXSliderView.h"
#import "LXViewController.h"

@interface LXSliderView()

@property (nonatomic, strong) NSString *firstImage;
@property (nonatomic, assign) int imageCount;

@end

@implementation LXSliderView

- (UIPageControl *)pageControl
{
    if(_pageControl == nil){
        
        _pageControl = [[UIPageControl alloc]init];
        
        _pageControl.currentPageIndicatorTintColor = LXColor(253, 98, 42);
        _pageControl.pageIndicatorTintColor = LXColor(189, 189, 189);
        //关闭用户交互
        _pageControl.userInteractionEnabled = NO;
    }
    
    return _pageControl;
}

/**
 *  快速创建轮播视图
 *
 *  @param firstImage 第一张轮播图片
 *  @param imageCount 所有的轮播图片数
 *  @param target 代理
 *
 */
- (instancetype)initWithFirstImage:(NSString *)firstImage imageCount:(int)imageCount
{
    self.firstImage = firstImage;
    self.imageCount = imageCount;
    return [self init];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    
    if(self){
        //设置SliderView尺寸
//        UIImage *image = [UIImage imageWithName:self.firstImage];
//        CGFloat sliderViewW = image.size.width;
//        CGFloat sliderViewH = image.size.height;
//        
//        self.frame = CGRectMake(0, 0, sliderViewW, sliderViewH);
        self.frame = CGRectMake(0, 0, [UIScreen mainScreen].applicationFrame.size.width, [UIScreen mainScreen].applicationFrame.size.height);

        //设置滚动图片
        for (int index = 0; index<self.imageCount; index++){
            UIImageView *imageView = [[UIImageView alloc]init];
            //获取相同的图片名
            NSString *str = self.firstImage;
            NSString *sameName = [str substringWithRange:NSMakeRange(0, str.length-1)];
            
            NSString *imageName = [NSString stringWithFormat:@"%@%d",sameName,index + 1];
            imageView.image = [UIImage imageWithName:imageName];
            
            if(index == 2){
                [self setupImageView:imageView];
            }
            
            [self addSubview:imageView];
        }
        
        //设置滚动范围
        self.contentSize = CGSizeMake(self.frame.size.width * 3, 0);
        //隐藏底部滚动条
        self.showsHorizontalScrollIndicator = NO;
        //开启页面识别
        self.pagingEnabled = YES;
        //去掉弹簧效果
        //self.bounces = NO;
    }
    return self;

}

- (void)layoutSubviews
{
    for (int index = 0; index<self.imageCount; index++) {
        UIImageView *imageView = self.subviews[index];
        
        //设置图片的frame
        CGFloat imageViewY = 0;
        CGFloat imageViewW = self.frame.size.width;
        CGFloat imageViewH = self.frame.size.height;
        CGFloat imageViewX = imageViewW * index;
        imageView.frame = CGRectMake(imageViewX, imageViewY, imageViewW, imageViewH);
        
        if(index == 2){
            UIButton *start = imageView.subviews[0];
            
            CGFloat startX = self.frame.size.width * 0.5;
            CGFloat startY = self.frame.size.height * 0.6;
            CGFloat startW = start.currentBackgroundImage.size.width;
            CGFloat startH = start.currentBackgroundImage.size.height;
            
            start.center = CGPointMake(startX, startY);
            start.bounds = CGRectMake(0, 0, startW, startH);
            
            UIButton *share = imageView.subviews[1];
            
            CGFloat shareX = self.frame.size.width * 0.5;
            CGFloat shareY = self.frame.size.height * 0.5;
            CGFloat shareW = 200;
            CGFloat shareH = 50;
            
            share.center = CGPointMake(shareX, shareY);
            share.bounds = CGRectMake(0, 0, shareW, shareH);
        }
    }
    
    //设置pageControl的frame
    _pageControl.center = CGPointMake(self.frame.size.width * 0.5, self.frame.size.height - 30);
    _pageControl.numberOfPages = self.imageCount;
    _pageControl.bounds = CGRectMake(0, 0, self.frame.size.width, 30);
}

//  设置第三张轮播图样式
- (void)setupImageView:(UIImageView *)imageView
{
    imageView.userInteractionEnabled = YES;
    
    //添加开始按钮
    UIButton *start = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [start setBackgroundImage:[UIImage imageWithName:@"new_feature_finish_button"] forState:UIControlStateNormal];
    [start setBackgroundImage:[UIImage imageWithName:@"new_feature_finish_button_highlighted"] forState:UIControlStateHighlighted];
    [start setTitle:@"立即体验" forState:UIControlStateNormal];
    [start addTarget:self action:@selector(startClick:) forControlEvents:UIControlEventTouchUpInside];
    
    [imageView addSubview:start];
    
    // 添加分享按钮
    UIButton *share = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [share setImage:[UIImage imageWithName:@"new_feature_share_true"] forState:UIControlStateNormal];
    [share setImage:[UIImage imageWithName:@"new_feature_share_false"] forState:UIControlStateSelected];
    [share setTitle:@"分享给你的好友" forState:UIControlStateNormal];
    [share setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [share addTarget:self action:@selector(shareClick:) forControlEvents:UIControlEventTouchUpInside];
    
    [imageView addSubview:share];

}

/**
 *  start按钮点击
 *
 */
- (void)startClick:(UIButton *)start
{
    self.window.rootViewController = [[LXViewController alloc]init];
}

/**
 *  分享按钮点击
 *
 */
- (void)shareClick:(UIButton *)share
{
    share.selected = !share.isSelected;
}
@end
